<?php /* ADMIN  $Id: vw_usr.php,v 1.19.10.1 2006/03/14 13:07:40 gregorerhardt Exp $ */
GLOBAL $dPconfig, $canEdit, $stub, $where, $orderby;

$q  = new DBQuery;
$q->addTable('users', 'u');
$q->addQuery('DISTINCT(user_id), user_username, contact_last_name, contact_first_name,
	permission_user, contact_email, company_name, contact_company');
$q->addJoin('contacts', 'con', 'user_contact = contact_id');
$q->addJoin('companies', 'com', 'contact_company = company_id');
$q->addJoin('permissions', 'per', 'user_id = permission_user');

if ($stub) {
	$q->addWhere("(UPPER(user_username) LIKE '$stub%' or UPPER(contact_first_name) LIKE '$stub%' OR UPPER(contact_last_name) LIKE '$stub%')");
} else if ($where) {
	$where = $q->quote("%$where%");
	$q->addWhere("(UPPER(user_username) LIKE $where or UPPER(contact_first_name) LIKE $where OR UPPER(contact_last_name) LIKE $where)");
}

$q->addGroup('user_id');
$q->addOrder($orderby);
$users = $q->loadList();
$canLogin = !isset($_REQUEST['inactive']) || $_REQUEST['inactive'] == 'false';
?>
<table cellpadding="7" cellspacing="0" border="0" width="100%" class="infopanel">
<tr>
<td>
<?
showButton('New User', '?m=admin&a=addedituser');
?>
</td>
<td align="right">
<table class="internal">
	<tr><td>Show:</td>
	<td><input id="idInactive" type="checkbox" name="inactive" <?=$_REQUEST['inactive']=='true' ? 'checked="checked"' : ''?>
			onclick="go('?m=admin&a=index&orderby=<?=$_REQUEST['orderby']?>&inactive='+this.checked)"/></td>
	<td><label for="idInactive">inactive users</label></td></tr>
</table>
</td>
</tr>
</table>
<table cellpadding="2" cellspacing="0" border="0" width="100%" class="list">
<tr>
	<th width="36" align="right">&nbsp;</th>
	<?php if (dPgetParam($_REQUEST, "tab", 0) == 0){ ?>
	<th width="150" colspan="2"><?=$AppUI->_('Login History')?></th>
	<?php } ?>
	<th width="80">
		<a href="?m=admin&a=index&orderby=user_username"><?=$AppUI->_('Login Name')?></a>
	</th>
	<th>
		<a href="?m=admin&a=index&orderby=contact_last_name"><?=$AppUI->_('Real Name')?></a>
	</th>
	<th>
		<a href="?m=admin&a=index&orderby=contact_company"><?=$AppUI->_('Company')?></a>
	</th>
	<th width="16px" class="last">&nbsp;</th>
</tr>
<?php

$perms =& $AppUI->acl();
$nu = 0;
foreach ($users as $row) {
	if ($perms->isUserPermitted($row['user_id']) != $canLogin)
		continue;
	$nu++;
?>
<tr class="second">
	<td align="right" nowrap="nowrap">
<?php if ($canEdit) { ?>
		<a href="?m=admin&a=viewuser&user_id=<?=$row['user_id']?>&tab=3" title="<?=$AppUI->_('edit permissions')?>"><img src="images/role.gif"></a>&nbsp;&nbsp;<a href="index.php?m=admin&a=addedituser&user_id=<?=$row['user_id']?>" title="<?=$AppUI->_('edit')?>"><img src="images/edit.gif"/></a>
<?php } ?>
	</td>
	<?php if (dPgetParam($_REQUEST, "tab", 0) == 0){ ?>
<?php
	$q  = new DBQuery;
	$q->addTable('user_access_log', 'ual');
	$q->addQuery('user_access_log_id, ROUND((unix_timestamp(now()) - unix_timestamp(date_time_in))/3600,2) as hours');
	$q->addQuery('ROUND((unix_timestamp(now()) - unix_timestamp(date_time_last_action))/3600, 2) as idle');
	$q->addQuery('if(isnull(date_time_out) or date_time_out = "0000-00-00 00:00:00","1","0") as online');
	$q->addWhere('user_id = '.$row['user_id']);
	$q->addOrder('user_access_log_id DESC');
	$q->setLimit(1);
	$user_logs = $q->loadList();

	if ($user_logs)
		foreach ($user_logs as $row_log) {
			if ($row_log['online'] == '1'){
				echo '<td style="color: green">'.$AppUI->_('Online').': '.$row_log['hours'].' '.$AppUI->_('hrs.').'</td>'.
					 '<td style="color: green">'.$AppUI->_('Idle').': '.$row_log['idle'].' '. $AppUI->_('hrs.').'</td>';
			} else {
				echo '<td colspan="2" style="color: red">'.$AppUI->_('Offline').'</td>';
			}
		}
	else
		echo '<td colspan="2" style="color: grey">'.$AppUI->_('Never Visited').'</td>';
	}?>
	<td>
		<a href="index.php?m=admin&a=viewuser&user_id=<?=$row['user_id']?>"><?=$row['user_username']?></a>
	</td>
	<td>
		<a href="mailto:<?=$row['contact_email']?>"><img src="images/mail.gif" title="email">
		<?php
		if ($row['contact_last_name'] && $row['contact_first_name'])
			echo $row['contact_first_name'].' '.$row['contact_last_name'];
		else
			echo '<span style="font-style: italic">unknown</span>';
		?>
		</a>
	</td>
	<td>
		<a href="index.php?m=companies&a=view&company_id=<?=$row['contact_company']?>"><?=$row['company_name']?></a>
	</td>
	<td align="center">
		<?php
			$user_display = trim(addslashes($row['contact_first_name'].' '.$row['contact_last_name']));
			if (empty($user_display))
				$user_display = $row['user_username'];
		?>
		<a href="javascript:delMe(<?=$row['user_id']?>, '<?=$user_display?>')" title="<?=$AppUI->_('delete')?>"><img src="images/delete.gif"/></a>
	</td>
</tr>
<?}

if ($nu == 0) {
	showEmptySpanRow('second');
}
?>

</table>
